<template>
  <div class="login-form-wrapper">
    <div class="login-form-title">IP管理员登录</div>
    <a-form
      ref="loginForm"
      :model="userInfo"
      class="login-form"
      layout="vertical"
      @submit="handleSubmit"
    >
      <a-form-item field="phone" hide-label>
        <a-input
          v-model="userInfo.phone"
          size="large"
          placeholder="请输入手机号"
          allow-clear
        >
          <template #prefix>
            <icon-mobile />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item field="password" hide-label>
        <a-input-password
          v-model="userInfo.password"
          placeholder="请输入密码"
          allow-clear
          size="large"
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>
      <a-space :size="16" direction="vertical">
        <div class="login-form-password-actions">
          <a-checkbox
            checked="rememberPassword"
            :model-value="loginConfig.rememberPassword"
            @change="setRememberPassword as any"
          >
            记住登录状态
          </a-checkbox>
        </div>
        <a-button
          type="primary"
          html-type="submit"
          long
          :loading="loading"
          size="large"
        >
          登录
        </a-button>
      </a-space>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
  import { reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import { useStorage } from '@vueuse/core';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import { Md5 } from 'ts-md5';

  const router = useRouter();
  const { loading, setLoading } = useLoading();
  const userStore = useUserStore();

  const loginConfig = useStorage('login-config', {
    rememberPassword: true,
    phone: '',
    password: '',
  });
  const userInfo = reactive({
    phone: loginConfig.value.phone,
    password: loginConfig.value.password,
  });

  const handleSubmit = async ({
    errors,
    values,
  }: {
    errors: Record<string, ValidatedError> | undefined;
    values: Record<string, any>;
  }) => {
    if (loading.value) return;
    if (!errors) {
      if (!values.phone) {
        Message.error('请输入手机号');
        return;
      }
      if (!values.password) {
        Message.error('请输入密码');
        return;
      }
      setLoading(true);
      try {
        await userStore.login({
          phone: values.phone,
          password: Md5.hashStr(values.password),
        });
        Message.success('登录成功');
        const { rememberPassword } = loginConfig.value;
        const { phone, password } = values;
        // 实际生产环境需要进行加密存储。
        // The actual production environment requires encrypted storage.
        loginConfig.value.phone = rememberPassword ? phone : '';
        loginConfig.value.password = rememberPassword ? password : '';
        const { redirect, ...othersQuery } = router.currentRoute.value.query;
        router.push({
          name: (redirect as string) || 'GameListApp',
          query: {
            ...othersQuery,
          },
        });
      } finally {
        setLoading(false);
      }
    }
  };
  const setRememberPassword = (value: boolean) => {
    loginConfig.value.rememberPassword = value;
  };
</script>

<style lang="less" scoped>
  .login-form {
    &-wrapper {
      padding: 30px;
      width: 500px;
      background: #fff;
      border-radius: 10px;
    }

    &-title {
      color: var(--color-text-1);
      font-weight: 500;
      margin-bottom: 30px;
      font-size: 24px;
      line-height: 32px;
    }

    &-password-actions {
      display: flex;
      justify-content: space-between;
    }

    &-register-btn {
      color: var(--color-text-3) !important;
    }
  }
</style>
